package com.paobuji.aidebao.myapplication.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jaeger.library.StatusBarUtil;
import com.paobuji.aidebao.myapplication.R;
import com.paobuji.aidebao.myapplication.activity.BaseActivity;
import com.paobuji.aidebao.myapplication.appinterface.GetInterfaceObjctListener;
import com.paobuji.aidebao.myapplication.bean.UserBeanResult;
import com.paobuji.aidebao.myapplication.tool.ACache;
import com.paobuji.aidebao.myapplication.tool.GetAppInfoUtil;
import com.paobuji.aidebao.myapplication.tool.MyIntentTools;
import com.paobuji.aidebao.myapplication.view.CircleImageView;
import com.paobuji.aidebao.myapplication.view.CustomTitlebar;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyUserActivity extends BaseActivity {

    @BindView(R.id.myUserImage)
    CircleImageView myUserImage;
    @BindView(R.id.userName)
    TextView userName;
    @BindView(R.id.allDisText)
    TextView allDisText;
    @BindView(R.id.sportPlanImg)
    ImageView sportPlanImg;
    @BindView(R.id.sportPlanText)
    TextView sportPlanText;
    @BindView(R.id.sportPlanLayout)
    RelativeLayout sportPlanLayout;
    @BindView(R.id.sportHistoryImg)
    ImageView sportHistoryImg;
    @BindView(R.id.sportHistoryText)
    TextView sportHistoryText;
    @BindView(R.id.sportHistoryLayout)
    RelativeLayout sportHistoryLayout;
    @BindView(R.id.sportSetImg)
    ImageView sportSetImg;
    @BindView(R.id.sportSetText)
    TextView sportSetText;
    @BindView(R.id.sportSetLayout)
    RelativeLayout sportSetLayout;
    @BindView(R.id.activity_my_user)
    RelativeLayout activityMyUser;

    Context context;
    Intent intent;
    @BindView(R.id.mainTitle)
    CustomTitlebar mainTitle;

    UserBeanResult.DataBean.UserBean userBean;
    @BindView(R.id.topLinearLayout)
    LinearLayout topLinearLayout;
    @BindView(R.id.centerLinearLayout)
    LinearLayout centerLinearLayout;
//    @BindView(R.id.systemMessage)
//    TextView systemMessage;
    ACache aCache;


    int ranking = 0;
    @BindView(R.id.sportRankingImg)
    ImageView sportRankingImg;
    @BindView(R.id.sportRankingText)
    TextView sportRankingText;
    @BindView(R.id.sportRankingLayout)
    RelativeLayout sportRankingLayout;
    MyIntentTools myIntentTools;

    DecimalFormat decimalFormat;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_user);
        ButterKnife.bind(this);
        //设置全透明
        StatusBarUtil.setTransparent(this);
        context = getApplicationContext();
        decimalFormat = new DecimalFormat("0.00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
        aCache = ACache.get(context);
        myIntentTools = new MyIntentTools();
        userBean = new UserBeanResult.DataBean.UserBean();
        //在缓存中获取用户数据
        userBean = (UserBeanResult.DataBean.UserBean) aCache.getAsObject("userBean");
        //判断用户数据是否拿到，若没有的话从服务器拉取
        if (userBean != null) {

            if(!"".equals(userBean.getUserphone()) && userBean.getUserphone() != null){
                myIntentTools.selectUserByPhone(context,userBean.getUserphone());
                myIntentTools.setGetInterfaceObjctListener(new GetInterfaceObjctListener() {
                    @Override
                    public void getObjectDate(Object data) {
                        if(((UserBeanResult) data).getCode() == 0){
                           // userBean = ((UserBeanResult) data).getData().getUser();
                            ranking = ((UserBeanResult) data).getData().getRanking();
                            setViewText();
                        }else{
                            getUserByIdentified();
                        }

                    }
                });
            }else{                                    //65    --->>48             +63
                getUserByIdentified();
            }
        } else{
            getUserByIdentified();
        }

        //标题栏事件监测
        mainTitle.setAction(new CustomTitlebar.TitleBarOnClickListener() {
            @Override
            public void performAction(View view) {
                switch (view.getId()) {
                    case R.id.iv_left:
                        finish();
                        //Toast.makeText(MainActivity.this, "左边图片按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.tv_left:
                        finish();
                        //Toast.makeText(MainActivity.this, "左边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.iv_right:
                        goHelp();
                        //Toast.makeText(MainActivity.this, "右边图片按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.tv_right:
                        goHelp();
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }

    public void getUserByIdentified(){
//        myIntentTools.loginUserByIdentified(context, GetAppInfoUtil.getUniquePsuedoID());
//        myIntentTools.setGetInterfaceObjctListener(new GetInterfaceObjctListener() {
//            @Override
//            public void getObjectDate(Object data) {
//                userBean = ((UserBeanResult) data).getData().getUser();
//                ranking = ((UserBeanResult) data).getData().getRanking();
//                setViewText();
//            }
//        });
    }
    //更新界面
    public void setViewText() {
//        Glide.with(this)
//                .load(userBean.getUserImg())
//                .asBitmap()
//                .skipMemoryCache(false) //跳过内存缓存
//                .diskCacheStrategy(DiskCacheStrategy.SOURCE)     //缓存设置
//                .priority(Priority.NORMAL)    //优先级  LOW     NORMAL      HIGH      IMMEDIATE
//                .placeholder(R.mipmap.aipao)    //占位图
//                .error(R.mipmap.aipao)
//                .into(myUserImage);

        if ("".equals(userBean.getUsername()) || userBean.getUsername() == null) {
//            userBean.setUsername("MAXFREE");
        }
//        userName.setText(userBean.getUsername());
        allDisText.setText(getResources().getString(R.string.alDis)+ ":" +  decimalFormat.format(userBean.getAllDis()) + getString(R.string.unit2) );//+ getResources().getString(R.string.world) + ":" + ranking
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.myUserImage, R.id.userName, R.id.allDisText, R.id.sportPlanImg, R.id.sportPlanText,
            R.id.sportPlanLayout, R.id.sportHistoryImg, R.id.sportHistoryText, R.id.sportHistoryLayout,
            R.id.sportSetImg, R.id.sportSetText, R.id.sportSetLayout,R.id.sportRankingImg,
            R.id.sportRankingText, R.id.sportRankingLayout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.myUserImage:
                goSetUserInfo();
                break;
            case R.id.userName:
                goSetUserInfo();
                break;
            case R.id.allDisText:
                goSetUserInfo();
                break;
            case R.id.sportPlanImg:
                goSetPlan();
                break;
            case R.id.sportPlanText:
                goSetPlan();
                break;
            case R.id.sportPlanLayout:
                goSetPlan();
                break;
            case R.id.sportHistoryImg:
                goSportHistory();
                break;
            case R.id.sportHistoryText:
                goSportHistory();
                break;
            case R.id.sportHistoryLayout:
                goSportHistory();
                break;
            case R.id.sportSetImg:
                goSet();
                break;
            case R.id.sportSetText:
                goSet();
                break;
            case R.id.sportSetLayout:
                goSet();
                break;
            case R.id.sportRankingImg:
                goRanking();
                break;
            case R.id.sportRankingText:
                goRanking();
                break;
            case R.id.sportRankingLayout:
                goHelp();

                break;
//            case R.id.systemMessage:
//                toastLog("no message");
//                break;
        }
    }

    /**
     * 个人信息设置
     */
    public void goSetUserInfo() {
        Log.i("用户id", userBean.getId() + "----");
//        if ("".equals(userBean.getUserphone()) || userBean.getUserphone() == null) {
//            intent = new Intent(MyUserActivity.this, LoginActivity.class);
//            startActivity(intent);
//        } else {
//            intent = new Intent(MyUserActivity.this, MyUserInfoSetActivity.class);
//            startActivity(intent);
//        }
    }

    /**
     * 目标设置
     */
    public void goSetPlan() {
        intent = new Intent(MyUserActivity.this, MySportPlanSetActivity.class);
        startActivity(intent);
    }

    /**
     * 运动记录
     */
    public void goSportHistory() {
        intent = new Intent(MyUserActivity.this, MySportHistoryActivity.class);
        startActivity(intent);
    }

    /**
     * 跟多设置
     */
    public void goSet() {
        intent = new Intent(MyUserActivity.this, MySetActivity.class);
        startActivity(intent);
    }

    /**
     * 世界排名
     */
    public void goRanking(){
//        intent = new Intent(MyUserActivity.this, MyRankingActivity.class);
//        startActivity(intent);
    }

    /**
     * 前往帮助界面
     */
    public void goHelp(){
        intent = new Intent(MyUserActivity.this, MyHelpActivity.class);
        startActivity(intent);
    }



}
